﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DVSProject.Models;
using DVSProject.Lib;

namespace DVSProject.Controllers
{
    public class WashFactorRemarkController : Controller
    {
        //
        // GET: /WashFactorRemark/

        public ActionResult Index()
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            WashFactorRemarkRepo _WashFactorRemarkRepo = new WashFactorRemarkRepo();
            IEnumerable<WashFactorRemarkModels> _WashFactorRemarks = _WashFactorRemarkRepo.GetWashFactorRemark();
            return View(_WashFactorRemarks);
        }

        //
        // GET: /WashFactorRemark/Details/5

        public ActionResult Details(int id)
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            WashFactorRemarkRepo _WashFactorRemarkRepo = new WashFactorRemarkRepo();
            WashFactorRemarkModels _WashFactorRemark = new WashFactorRemarkModels();
            _WashFactorRemark = _WashFactorRemarkRepo.GetWashFactorRemarkById(id);
            return View(_WashFactorRemark);
        }

        //
        // GET: /WashFactorRemark/Create

        public ActionResult Create()
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            WashFactorRemarkModels _WashFactorRemark = new WashFactorRemarkModels();

            TitleWashFactorsRepo _TitleWashFactorsRepo = new TitleWashFactorsRepo();
            IEnumerable<TitleWashFactorsModels> _TitleWashFactorss = _TitleWashFactorsRepo.GetTitleWashFactorsDropDown();
            string selectedId = "อุณหภูมิ";
            IEnumerable<SelectListItem> selectlist = from s in _TitleWashFactorss
                                                     select new SelectListItem
                                                     {
                                                         Selected = (s.TitleWashFactorsType == selectedId),
                                                         Text = s.TitleWashFactorsType,
                                                         Value = s.TitleWashFactorsType.ToString()
                                                     };

            _WashFactorRemark.TitleSelect = selectlist;
            return View(_WashFactorRemark);
        } 

        //
        // POST: /WashFactorRemark/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            try
            {
                // TODO: Add insert logic here
                WashFactorRemarkRepo _WashFactorRemarkRepo = new WashFactorRemarkRepo();
                WashFactorRemarkModels _WashFactorRemark = new WashFactorRemarkModels();

                _WashFactorRemark.TitleWashFactorsType = collection["TitleWashFactorsType"];
                _WashFactorRemark.WashFactorRemarkTh = collection["WashFactorRemarkTh"];
                _WashFactorRemark.WashFactorRemarkEng = collection["WashFactorRemarkEng"];
                _WashFactorRemark.CreateBy = (string)Session["User"];

                bool result = _WashFactorRemarkRepo.Create(_WashFactorRemark);
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        
        //
        // GET: /WashFactorRemark/Edit/5
 
        public ActionResult Edit(int id)
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            WashFactorRemarkRepo _WashFactorRemarkRepo = new WashFactorRemarkRepo();
            WashFactorRemarkModels _WashFactorRemark = new WashFactorRemarkModels();
            _WashFactorRemark = _WashFactorRemarkRepo.GetWashFactorRemarkById(id);

            TitleWashFactorsRepo _TitleWashFactorsRepo = new TitleWashFactorsRepo();
            IEnumerable<TitleWashFactorsModels> _TitleWashFactorss = _TitleWashFactorsRepo.GetTitleWashFactorsDropDown();
            string selectedId = "อุณหภูมิ";
            IEnumerable<SelectListItem> selectlist = from s in _TitleWashFactorss
                                                     select new SelectListItem
                                                     {
                                                         Selected = (s.TitleWashFactorsType == selectedId),
                                                         Text = s.TitleWashFactorsType,
                                                         Value = s.TitleWashFactorsType.ToString()
                                                     };

            _WashFactorRemark.TitleSelect = selectlist;
            return View(_WashFactorRemark);
      
        }

        //
        // POST: /WashFactorRemark/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            try
            {
                // TODO: Add update logic here
                WashFactorRemarkRepo _WashFactorRemarkRepo = new WashFactorRemarkRepo();
                WashFactorRemarkModels _WashFactorRemark = new WashFactorRemarkModels();
                _WashFactorRemark.Id = id;
                _WashFactorRemark.TitleWashFactorsType = collection["TitleWashFactorsType"];
                _WashFactorRemark.WashFactorRemarkTh = collection["WashFactorRemarkTh"];
                _WashFactorRemark.WashFactorRemarkEng = collection["WashFactorRemarkEng"];
                _WashFactorRemark.UpdateBy = (string)Session["User"];
                bool result = _WashFactorRemarkRepo.Edit(_WashFactorRemark);
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /WashFactorRemark/Delete/5
 
        public ActionResult Delete(int id)
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            WashFactorRemarkRepo _WashFactorRemarkRepo = new WashFactorRemarkRepo();
            WashFactorRemarkModels _WashFactorRemark = new WashFactorRemarkModels();
            _WashFactorRemark = _WashFactorRemarkRepo.GetWashFactorRemarkById(id);
            return View(_WashFactorRemark);
        }

        //
        // POST: /WashFactorRemark/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            try
            {
                // TODO: Add delete logic here
                WashFactorRemarkRepo _WashFactorRemarkRepo = new WashFactorRemarkRepo();
                WashFactorRemarkModels _WashFactorRemark = new WashFactorRemarkModels();
                _WashFactorRemark.Id = id;
                _WashFactorRemark.UpdateBy = (string)Session["User"];
                bool result = _WashFactorRemarkRepo.Delete(_WashFactorRemark);

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
